''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：alibaba_ad.py
#日期：2023-11-17
#备注：阿里巴巴国际站直通车情况
1. 推广计划产品
CREATE TABLE `alibaba_ad_group_product` (
  `adId` varchar(20) NOT NULL COMMENT '推广计划id',
  `productId` varchar(20) NOT NULL COMMENT '计划产品id',
  `productType` varchar(20) NOT NULL COMMENT '计划产品分类',
  `imageUrl` varchar(200) DEFAULT NULL COMMENT '计划产品图片',
  `subject` varchar(200) DEFAULT NULL COMMENT '计划产品标题',
  PRIMARY KEY (`adId`,`productId`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='阿里巴巴直通车推广计划';
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

from pycacho.cachobase.file_deal import replace_txt
from pacong.stock.stock_config import db,cursor
from pycacho.cachobase.logger import Logger
logger = Logger("alibaba_ad").get_log()

# 推广计划产品统计
def get_ad_group_batch():
    file = 'F:\\alibaba\json\\adGroupBatch.json'
    batch_list = replace_txt(file)
    for product in batch_list:
        adId = product['campaignId']  # 计划id
        productId = product['productId']  # 产品id
        productType = product['cateName']  # 产品分类
        imageUrl = product['imageUrl'].replace('_100x100.jpg','').replace('_100x100.png','')  # 产品图片
        subject = product['subject']  # 产品标题
        print(productId,subject,imageUrl)
        insert_ad_group_batch(adId,productId,productType,imageUrl,subject)

def insert_ad_group_batch(adId,productId,productType,imageUrl,subject):
    sql = "insert into alibaba_ad_group_product (adId,productId,productType,imageUrl,subject) " \
          "values ('%s','%s','%s','%s','%s');" \
          % (adId,productId,productType,imageUrl,subject)
    try:
        cursor.execute(sql)
    except Exception as e:
        # 发生错误时回滚
        db.rollback()
        print(str(e))
    else:
        db.commit()
        logger.info('阿里巴巴推广计划产品%s插入成功！', productId)


if __name__=='__main__':
    get_ad_group_batch()